//
//  SYChooseClassesController.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/7/15.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "SYChooseClassesController.h"
#import "ChooseGradeCell.h"
#import "ClassesModel.h"
#import "AssignHomeWorkViewController.h"
#import "PushNotiViewController.h"
#import "AssignStuHomeWorkViewController.h"
@interface SYChooseClassesController ()
{
    NSMutableArray *classArr;
    UIButton *allselectBtn;
    UIImageView *allselectImage;
    NSMutableArray *allClassArr;
    UIButton *sureBtn;
}
@end

@implementation SYChooseClassesController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"选择发送班级";
    self.view.backgroundColor=RGBACOLOR(244, 244, 244, 1);
    
    sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(0, 0, 60, 44);
    [sureBtn setTitleColor:RGBACOLOR(151, 151, 151, 1) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:[NSString stringWithFormat:@"确定"] forState:UIControlStateNormal];
    sureBtn.userInteractionEnabled=NO;
    sureBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    UIBarButtonItem *sureBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:sureBtn];
    self.navigationItem.rightBarButtonItem = sureBarBtnItem;
    
    
    classArr=[NSMutableArray array];
    allClassArr=[NSMutableArray array];
    if(_claArray.count>0)
    {
        for (NSDictionary *dic in _claArray) {
            ClassesModel *model=[ClassesModel mj_objectWithKeyValues:dic];
            [classArr addObject:model];
        }
       
    }
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(12, 18, SCREENWIDTH-24, 14)];
    label.text=@"选择一个班或多个班";
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=RGBACOLOR(151, 151, 151, 1);
    [self.view addSubview:label];

    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, label.frame.origin.y+label.frame.size.height+8, SCREENWIDTH, 0.5)];
    line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
    [self.view addSubview:line];
    
    UIView *topciew=[[UIView alloc]initWithFrame:CGRectMake(0, line.frame.origin.y+line.frame.size.height, VIEW_WIDTH, 42)];
    topciew.backgroundColor=[UIColor whiteColor];
    
    allselectImage=[[UIImageView alloc]initWithFrame:CGRectMake(12, 12, 18, 18)];
    allselectImage.image=[UIImage imageNamed:@"homeworks_allchoose_nor_"];
    
    UILabel *allLabel=[[UILabel alloc]initWithFrame:CGRectMake(allselectImage.frame.origin.x+allselectImage.frame.size.width +15, 14, 200, 16)];
    allLabel.text=@"全选";
    allLabel.font=[UIFont systemFontOfSize:16];
    [topciew addSubview:allLabel];
    [topciew addSubview:allselectImage];
    
    allselectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    allselectBtn.frame=CGRectMake(0, 0, SCREENWIDTH, 42);
    [allselectBtn addTarget:self action:@selector(allSelectedAction:) forControlEvents:UIControlEventTouchUpInside];
    [topciew addSubview:allselectBtn];
    [self.view addSubview:topciew];
    
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0,topciew.frame.origin.y+topciew.frame.size.height, VIEW_WIDTH, SCREENHEIGH-64-topciew.frame.origin.x-topciew.frame.size.height) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    _table.bounces=NO;
    _table.backgroundColor=RGBACOLOR(244, 244, 244, 1);
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_table];
    // Do any additional setup after loading the view.
}
-(void)allSelectedAction:(UIButton *)sender
{
    if (allClassArr.count<classArr.count) {
        [allClassArr removeAllObjects];
        for ( int i=0;i< classArr.count;i++) {
            ClassesModel *model=[classArr objectAtIndex:i];
            model.flag=YES;
            [allClassArr addObject:model];
            [classArr replaceObjectAtIndex:i withObject:model];
            
        }
        allselectImage.image=[UIImage imageNamed:@"homeworks_allchoose_press_"];

    }else
    {
        [allClassArr removeAllObjects];
        for ( int i=0;i< classArr.count;i++) {
            ClassesModel *model=[classArr objectAtIndex:i];
            model.flag=NO;
            [classArr replaceObjectAtIndex:i withObject:model];
            
        }

        allselectImage.image=[UIImage imageNamed:@"homeworks_allchoose_nor_"];
    }
    if (allClassArr.count>0) {
        sureBtn.userInteractionEnabled=YES;
        [sureBtn setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
        [sureBtn setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)allClassArr.count] forState:UIControlStateNormal];
    }else
    {
        sureBtn.userInteractionEnabled=NO;
        [sureBtn setTitleColor:RGBACOLOR(151, 151, 151, 1) forState:UIControlStateNormal];
        [sureBtn setTitle:[NSString stringWithFormat:@"确定"] forState:UIControlStateNormal];
    }
    
    
    [_table reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

        return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *content=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10)];
    content.backgroundColor=RGBACOLOR(244, 244, 244, 1);
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 0.5)];
    line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
    [content addSubview:line];
    UIView *line2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 0.5)];
    line2.backgroundColor=RGBACOLOR(199, 199, 204, 1);
    [content addSubview:line2];
    return content;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return classArr.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 42;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"cellid";
    ChooseGradeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (cell==nil) {
        cell = [[ChooseGradeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        
    }
   
   
        ClassesModel *model=[classArr objectAtIndex:indexPath.row];
        NSString *gradeStr=model.classname;
        cell.nameLabel.text=gradeStr;
        
        if (model.flag) {
            cell.selectimg.image=[UIImage imageNamed:@"homeworks_allchoose_press_"];
        }else
        {
            cell.selectimg.image=[UIImage imageNamed:@"homeworks_allchoose_nor_"];
        }
        if (indexPath.row==(classArr.count-1)) {
            cell.line.frame=CGRectMake(0, 41.5, SCREENWIDTH, 0.5);
        }
    
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
        ClassesModel *model=[classArr objectAtIndex:indexPath.row];
        model.flag=!model.flag;
        [classArr replaceObjectAtIndex:indexPath.row withObject:model];
         if(model.flag)
         {
             [allClassArr addObject:model];
         }else
         {
             [allClassArr removeObject:model];
         }
        if (allClassArr.count<classArr.count) {
            allselectImage.image=[UIImage imageNamed:@"homeworks_allchoose_nor_"];
        }
        if (allClassArr.count==classArr.count) {
            allselectImage.image=[UIImage imageNamed:@"homeworks_allchoose_press_"];
        }
    if (allClassArr.count>0) {
        sureBtn.userInteractionEnabled=YES;
        [sureBtn setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
        [sureBtn setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)allClassArr.count] forState:UIControlStateNormal];

    }else
    {
        sureBtn.userInteractionEnabled=NO;
        [sureBtn setTitleColor:RGBACOLOR(151, 151, 151, 1) forState:UIControlStateNormal];
        [sureBtn setTitle:[NSString stringWithFormat:@"确定"] forState:UIControlStateNormal];
    }
             [_table reloadData];
}
-(void)popAction:(UIButton *)sender
{
    AppDelegate *appdelegate=CONSTAPPDELEGATE;
    
    NSDictionary *dict=@{@"re_data":allClassArr,@"type":@"1"};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"senderNotification" object:nil userInfo:dict];
    NSLog(@"%@",super.navigationController.childViewControllers);
    for(UIViewController *vc in super.navigationController.childViewControllers)
    {
        
        if ([appdelegate.isNotOrHome isEqualToString:@"zy"]) {
            NSLog(@"%@",super.navigationController.childViewControllers);
            if ([vc isKindOfClass:[AssignStuHomeWorkViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }else if ([appdelegate.isNotOrHome isEqualToString:@"tz"])
        {
            if ([vc isKindOfClass:[PushNotiViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
